<?php
# thaodx@gmail.com

require_once("nuSOAP/lib/nusoap.php");
require_once("config.php");
require_once("functions.php");

$namespace = "MOReceive";

$server = new soap_server();
$server->configureWSDL($namespace);
$server->wsdl->schemaTargetNamespace = $namespace;

$server->register(  'Receive'
				  , array(  'moid'          => 'xsd:int'
						  , 'telco'         => 'xsd:string'
						  , 'service_num'   => 'xsd:string'
                          , 'phone'         => 'xsd:string'
                          , 'syntax'        => 'xsd:string'
                          , 'message'       => 'xsd:string')

				  , array('return'=>'xsd:string')
                  , $namespace
                  , false
                  , 'rpc'
                  , 'encoded'
                  , 'Receiving MO from SMS Gateway');

function Receive($moid, $telco, $service_num, $phone, $syntax, $message)
{
	global $conf;
	$arrReturn = array('ReceiveResult' => 0,  'MessageResponse' => 'Cu phap tin nhan khong chinh xac');
	if($moid != '' && $telco != '' && $service_num != '' && $phone != '' && $syntax != '' && $message != ''){
		// save to DB
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);
		$sqlInsert = "INSERT INTO tdx_sms_mo(mo_id, telco, service_num, phone, syntax, message_request, receive_date, status) VALUES
		('".intval($moid)."', '".cleanValue($telco)."', '".cleanValue($service_num)."', '".cleanValue($phone)."', '".cleanValue($syntax)."', '".cleanValue($message)."', NOW(), 1)";
		mysql_query($sqlInsert);
		if(preg_match('/'.cleanValue($syntax).' (.*)/is', $message, $match)){
			$sms_code = $match[1];
			$tmpService = substr($service_num, 0, 2);
			$smsCost = 0;
			$smsFTP = 0;
			switch($tmpService){
				case "87":
					$smsCost = 15000;
					$smsFTP = 6750;
					$smsUser = 4800;					
				break;
				case "86":
					$smsCost = 10000;
					$smsFTP = 4500;
					$smsUser = 3240;
				break;
				case "85":
					$smsCost = 5000;
					$smsFTP = 2250;
					$smsUser = 1620;
				break;
				case "84":
					$smsCost = 4000;
					$smsFTP = 1600;
					$smsUser = 1296;
				break;
				case "83":
					$smsCost = 3000;
					$smsFTP = 1200;
					$smsUser = 972;
				break;
				case "82":
					$smsCost = 2000;
					$smsFTP = 800;
					$smsUser = 648;
				break;
				case "81":
					$smsCost = 1000;
					$smsFTP = 350;
					$smsUser = 324;
				break;
				default:
					$smsCost = 500;
					$smsFTP = 0;
					$smsUser = 0;
				break;
			}
			if ((strtoupper($syntax) == "GN") && (strpos($sms_code,"khr") !== false)) {
				$tach = explode(" ", $sms_code);
				$maid = $tach[0];
				$maidpro = $tach[1];
				$idreg = trim(str_replace('khr','',$maid));
				$idreg = trim(str_replace('ma','',$idreg));
				$idreg = trim(str_replace('mj','',$idreg));
				$idreg = trim(str_replace('mi','',$idreg));				
				if ($service_num != "8000"){
					$sqlStat = "INSERT INTO sms_user_mo(mo_id, telco, service_num, phone, message_request, sms_cost, receive_date, type, id_reg, id_pro) VALUES('".intval($moid)."', '".cleanValue($telco)."', '".cleanValue($service_num)."', '".cleanValue($phone)."', '".$sms_code."', $smsUser, NOW(),'1', '$idreg', '$maidpro')";
				}else{
					$sqlStat = "INSERT INTO tdx_sms_stat(sms_syntax, sms_code, sms_cost, sms_ftp, receive_date) VALUES('".$syntax."', '".$sms_code."', $smsCost, $smsFTP, NOW())";
				}
			}else{
				$sqlStat = "INSERT INTO tdx_sms_stat(sms_syntax, sms_code, sms_cost, sms_ftp, receive_date) VALUES('".$syntax."', '".$sms_code."', $smsCost, $smsFTP, NOW())";
			}
			mysql_query($sqlStat);
		}
		mysql_close($con);
		switch (strtoupper($syntax))
			{
				case "AZ":
					$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
					mysql_select_db($conf['db_name']);
					if(strpos(strtoupper($message),"AZ XN") !== false) {
						$makichhoat = $message; 
						$makichhoat = trim(str_replace('az xn','',$makichhoat));
						$userphone = "0" . substr($phone, 2, strlen($phone) - 1);	
						$sqlSelect = "SELECT * FROM `sms_user` WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
						$query=mysql_query($sqlSelect);						
						if(mysql_num_rows($query) > 0)
						{
							$sqlStat = "UPDATE `smsimobizn_ws`.`sms_user` SET `bank` = '1' WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
							mysql_query($sqlStat);								
							$return = "0|Xac nhan thong tin cap nhat tai khoan thanh cong.";		
						}else{
							$return = "0|Tai khoan hoac so dien thoai cua ban chua duoc dang ky.";		
						}				
					}elseif(strpos(strtoupper($message),"AZ UI") !== false) {
						$makichhoat = $message; 
						$makichhoat = trim(str_replace('az ui','',$makichhoat));
						$userphone = "0" . substr($phone, 2, strlen($phone) - 1);	
						$sqlSelect = "SELECT * FROM `sms_user` WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
						$query=mysql_query($sqlSelect);						
						if(mysql_num_rows($query) > 0)
						{
							$sqlStat = "UPDATE `smsimobizn_ws`.`sms_user` SET `bank` = '1' WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
							mysql_query($sqlStat);								
							$return = "0|Xac nhan thong tin cap nhat tai khoan thanh cong.";		
						}else{
							$return = "0|Tai khoan hoac so dien thoai cua ban chua duoc dang ky.";		
						}				
					}else{						
						$makichhoat = $message; 
						$makichhoat = trim(substr($makichhoat, 2, strlen($makichhoat) - 1));
						$userphone = "0" . substr($phone, 2, strlen($phone) - 1);	
						$sqlSelect = "SELECT * FROM `sms_user` WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
						$query=mysql_query($sqlSelect);						
						if(mysql_num_rows($query) > 0)
						{
							$sqlStat = "UPDATE `smsimobizn_ws`.`sms_user` SET `status` = '1' WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
							mysql_query($sqlStat);								
							$return = "0|Kich hoat tai khoan su dung dich vu thanh cong.";		
						}else{
							$return = "0|Tai khoan hoac so dien thoai cua ban chua duoc dang ky.";		
						}				
					}
					mysql_close($con);
					return $return;
				case "GN":
					$tmpSer = substr($service_num, 2, 2);
					if ($tmpSer == "13"){
						$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
						mysql_select_db($conf['db_name']);
						if(strpos(strtoupper($message),"GN XN") !== false) {
							$makichhoat = $message; 
							$makichhoat = trim(str_replace('gn xn','',$makichhoat));
							$userphone = "0" . substr($phone, 2, strlen($phone) - 1);	
							$sqlSelect = "SELECT * FROM `sms_user` WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
							$query=mysql_query($sqlSelect);						
							if(mysql_num_rows($query) > 0)
							{
								$sqlStat = "UPDATE `smsimobizn_ws`.`sms_user` SET `bank` = '1' WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
								mysql_query($sqlStat);								
								$return = "0|Xac nhan thong tin cap nhat tai khoan '". $makichhoat ."' thanh cong.";		
							}else{
								$return = "0|Tai khoan hoac so dien thoai cua ban chua duoc dang ky.";		
							}				
						}elseif(strpos(strtoupper($message),"GN UI") !== false) {
							$makichhoat = $message; 
							$makichhoat = trim(str_replace('gn ui','',$makichhoat));
							$userphone = "0" . substr($phone, 2, strlen($phone) - 1);	
							$sqlSelect = "SELECT * FROM `sms_user` WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
							$query=mysql_query($sqlSelect);						
							if(mysql_num_rows($query) > 0)
							{
								$sqlStat = "UPDATE `smsimobizn_ws`.`sms_user` SET `status` = '1' WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
								mysql_query($sqlStat);								
								$return = "0|Xac nhan kich hoat tao tai khoan '". $makichhoat ."' thanh cong.";		
							}else{
								$return = "0|Tai khoan hoac so dien thoai cua ban chua duoc dang ky.";		
							}				
						}else{						
							$makichhoat = $message; 
							$makichhoat = trim(substr($makichhoat, 2, strlen($makichhoat) - 1));
							$userphone = "0" . substr($phone, 2, strlen($phone) - 1);	
							$sqlSelect = "SELECT * FROM `sms_user` WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
							$query=mysql_query($sqlSelect);						
							if(mysql_num_rows($query) > 0)
							{
								$sqlStat = "UPDATE `smsimobizn_ws`.`sms_user` SET `status` = '1' WHERE `sms_user`.`user_name` = '". $makichhoat ."' and `sms_user`.`mobile` = '" . $userphone . "'";
								mysql_query($sqlStat);								
								$return = "0|Kich hoat tai khoan '". $makichhoat ."' su dung dich vu thanh cong.";		
							}else{
								$return = "0|Tai khoan hoac so dien thoai cua ban chua duoc dang ky.";		
							}				
						}
						mysql_close($con);
						return $return;
					}
					if(strpos($message,"GN khbm1982746663") !== false) {
						return "0|Link xem clip http://bit.ly/SuWDgF Hoac de tai game cho di dong http://bit.ly/U0NPPs";						
					}elseif ($message == 'GN TestPort'){
						return "0|50000:cong port mobile";
					}elseif ($message == 'gn ws'){
						return "0|WS FPT Thanh cong";
					}else{						
						return "0|Chuc mung ban da kich hoat thanh cong. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					}
				case "GQ":
					if(strpos($message,"gq apa") !== false){
						return "0|Ban da kich hoat thanh cong ung dung doc truyen. Truy cap http://store4vn.mobi de tai ve cac truyen hay khac cho dien thoai.";
					}elseif(strpos($message,"gq api") !== false){
						$makichhoat = $message; 
						$makichhoat = str_replace('gq api','',$makichhoat);
						$makichhoat = trim($makichhoat);
						$makichhoat = base64_encode(strtoupper($makichhoat));
						return "0|Ma kich hoat: " . $makichhoat . " . Copy ma kich hoat vao khung Mua Truyen trong ung dung de doc tiep. Xem them truyen tai http://store4vn.mobi";						
					}elseif ($message == 'gq tad'){
						if ($service_num == "8700"){
							return "0|http://www.mediafire.com/?z69va6o24pmd8dd";
						}else{							
							return "0|Chuc mung ban da kich hoat thanh cong. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
						}
					}elseif ($message != 'gq tad'){						
						return "0|Chuc mung ban da kich hoat thanh cong. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					}else{
						return "0|Chuc mung ban da kich hoat thanh cong. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
					}
				case "B":
					return "0|Chuc mung ban da kich hoat thanh cong. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
				case "AB":
					return "0|Doi WS thanh cong";
				//Dich vu ve sms diem thi
				case "DQ":
					return check_dq($moid, $telco, $phone, $message);		
				case "DA":
					return check_da($moid, $telco, $service_num, $syntax, $phone, $message);
				case "DIEM":
					return check_diem($moid, $telco, $service_num, $syntax, $phone, $message);
				case "TN":
					return check_totnghiep($moid, $telco, $service_num, $syntax, $phone, $message);
				case "TD":
					return check_diemdh($moid, $telco, $service_num, $syntax, $phone, $message);
				case "XD":
					return check_diemdh($moid, $telco, $service_num, $syntax, $phone, $message);
				case "XDC":
					return check_diemchuan($moid, $telco, $service_num, $syntax, $phone, $message);
				case "TLC":
					return check_tylechoi($moid, $telco, $service_num, $syntax, $phone, $message);
			}

		return "0|Chuc mung ban da kich hoat thanh cong. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
	}
	return "0|Chuc mung ban da kich hoat thanh cong. Xem them video va Tai game hot mien phi tai http://imobiz.net/";
	//"0|message  - 0| - tinh cuoc viettel / 1| - Khong tinh cuoc"
}
       
//Them phan tra ve Dap An - De Thi Tot Nghiep PTTH va Dai Hoc
function check_dq($moid, $telco, $phone, $message)
{	
		global $conf;	
		$cuphap = substr($message, 2, strlen($message) - 1);
		$cuphap = trim($cuphap);
		$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://diemthi.azone.vn/tuyensinh.html";
		$sqlSelect = "1";
		$ins = 1;
		$table = "tbl_dapan_mo";
		if(strpos($cuphap,"bq") !== false) {
			//DQ BQ  Tra ve ket qua bi quyet mua thi
			if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
				$sqlSelect = "Select tieude, noidung, link From tbl_biquyet Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			}
			if(strpos($cuphap,"apa") !== false) {
				$cuphap = str_replace(' apa','',$cuphap);
				$cuphap = trim($cuphap);
				$sqlSelect = "Select tieude, noidung, link From tbl_biquyet Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			}
			$sqlSelect = "Select tieude, noidung, link From tbl_biquyet Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$ins = 0;
		}elseif(strpos($cuphap,"tlc") !== false) {
			//DQ BQ  Tra ve ket qua ty le choi
			if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			if(strpos($cuphap,"apa") !== false) {
				$cuphap = str_replace(' apa','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_tilechoi Where CuPhap = '" . trim(substr($cuphap, 3, strlen($cuphap) - 1)) . "'";
			$table = "tbl_tilechoi_mo";
		}elseif(strpos($cuphap,"dc") !== false) {
			//DQ DC  Tra ve ket qua diem chuan
			/*if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$table = "tbl_diemchuan_mo";*/
		}elseif(strpos($cuphap,"dt") !== false) {
			//DQ DT  Tra ve ket qua do truot cua thi sinh
			/*if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$table = "tbl_diemchuan_mo";*/
		}elseif(strpos($cuphap,"xh") !== false) {
			//DQ XH  Tra ve ket qua xep hang cua thi sinh
			/*if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$table = "tbl_diemchuan_mo";*/
		}elseif(strpos($cuphap,"dd") !== false) {
			//DQ DC  Tra ve ket qua dia diem thi dh
			/*if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$table = "tbl_diemchuan_mo";*/	
		}elseif(strpos($cuphap,"tk") !== false) {
			//DQ DC  Tra ve ket qua thu khoa cua truong dh
			if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			if(strpos($cuphap,"apa") !== false) {
				$cuphap = str_replace(' apa','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$table = "tbl_diemchuan_mo";			
		}elseif(strpos($cuphap,"n2") !== false) {
			//DQ DC  Tra ve ket qua nguyen vong 2
			if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			if(strpos($cuphap,"apa") !== false) {
				$cuphap = str_replace(' apa','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$table = "tbl_diemchuan_mo";			
		}elseif(strpos($cuphap,"n3") !== false) {
			//DQ DC  Tra ve ket qua nguyen vong 3
			if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			if(strpos($cuphap,"apa") !== false) {
				$cuphap = str_replace(' apa','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$table = "tbl_diemchuan_mo";			
		}elseif(strpos($cuphap,"ct") !== false) {
			//DQ BQ  Tra ve ket qua chi tieu tuyen sinh
			if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			if(strpos($cuphap,"apa") !== false) {
				$cuphap = str_replace(' apa','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_chitieu Where CuPhap = '" . trim(substr($cuphap, 2, strlen($cuphap) - 1)) . "'";
			$table = "tbl_chitieu_mo";
			
		}elseif(strpos($cuphap,"tn") !== false) {
			//DQ TN Tra ve Dap An De Thi Tot Nghiep PTTH
			if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			if(strpos($cuphap,"apa") !== false) {
				$cuphap = str_replace(' apa','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_dapan Where CuPhap = '" . strtoupper($cuphap) . "'";
			$table = "tbl_dapan_mo";
			
		}else{
			//Tra ve Dap An De Thi Dai Hoc
			if(strpos($cuphap,"app") !== false) {
				$cuphap = str_replace(' app','',$cuphap);
				$cuphap = trim($cuphap);
			}
			if(strpos($cuphap,"apa") !== false) {
				$cuphap = str_replace(' apa','',$cuphap);
				$cuphap = trim($cuphap);
			}
			$sqlSelect = "Select tieude, noidung, link, status From tbl_dapan Where CuPhap = '" . strtoupper($cuphap) . "'";
			$table = "tbl_dapan_mo";
		}
		
		if ($sqlSelect != "1"){
			//$return = $sqlSelect;
			$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
			mysql_select_db($conf['db_name']);	
			$query=mysql_query($sqlSelect);
			
			if(mysql_num_rows($query) > 0)
			{	
				while($row=mysql_fetch_array($query))	
				{	
					$return = $row['noidung'] . "... Xem them tai " . $row['link'];	
					//Kiem tra neu chua co status = 0 thi se insert vao bang tuong ung de gui tin nhan khi trang thai duoc cap nhat status = 1
					//$ins =  1 thi xet trang thai status
					if ( $ins == 1){
						if ($row['status'] == 0 ){
							$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES		('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
						}
					}
					
				}
			}else{
					if ( $ins == 1){
						$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES
			('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
					}
			}	
			if ($ins == 1) {
				mysql_query($sqlInsert);
			}
			mysql_close($con);
		}
		return "0|". $return;
}

//Them phan tra ve Diem thi - MaTinh or MaTruong - phan theo bang du lieu rieng
//TLC xem tra ve ty le choi 
function check_tylechoi($moid, $telco, $service_num, $syntax, $phone, $message)
{
	global $conf;	
	$cuphap = substr($message, 3, strlen($message) - 1);
	$cuphap = trim($cuphap);
	$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://diemthi.azone.vn/tuyensinh.html";
	
	//XDC Tra ve ket qua diem chuan
	if(strpos($cuphap,"app") !== false) {
		$cuphap = str_replace('app','',$cuphap);
		$cuphap = trim($cuphap);
	}
	if(strpos($cuphap,"apa") !== false) {
		$cuphap = str_replace(' apa','',$cuphap);
		$cuphap = trim($cuphap);
	}
	$sqlSelect = "Select tieude, noidung, link, status From tbl_tilechoi Where CuPhap = '" . strtoupper($cuphap) . "'";
	$table = "tbl_tilechoi_mo";
	
	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
	mysql_select_db($conf['db_name']);	
	$query=mysql_query($sqlSelect);
	
	if(mysql_num_rows($query) > 0)
	{	
		while($row=mysql_fetch_array($query))	
		{	
			$return = $row['noidung'] . "... Xem them tai " . $row['link'];	
			//Kiem tra neu chua co status = 0 thi se insert vao bang tuong ung de gui tin nhan khi trang thai duoc cap nhat status = 1
			if ($row['status'] == 0 ){
				$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES		('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
			}
		}
	}else{
		$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES
	('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
	}	
	
	mysql_query($sqlInsert);
	
	mysql_close($con);
	return "0|". $return;
}
//XDC xem tra ve diem chuan
function check_diemchuan($moid, $telco, $service_num, $syntax, $phone, $message)
{
	global $conf;	
	$cuphap = substr($message, 3, strlen($message) - 1);
	$cuphap = trim($cuphap);
	$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://diemthi.azone.vn/tuyensinh.html";
	
	//XDC Tra ve ket qua diem chuan
	if(strpos($cuphap,"app") !== false) {
		$cuphap = str_replace('app','',$cuphap);
		$cuphap = trim($cuphap);
	}
	if(strpos($cuphap,"apa") !== false) {
		$cuphap = str_replace(' apa','',$cuphap);
		$cuphap = trim($cuphap);
	}
	$sqlSelect = "Select tieude, noidung, link, status From tbl_diemchuan Where CuPhap = '" . strtoupper($cuphap) . "'";
	//$return = "Ban da gui tin nhan thanh cong. Xem them thong tin mua thi tai http://diemthi.azone.vn/";
	$table = "tbl_diemchuan_mo";
	
	$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
	mysql_select_db($conf['db_name']);	
	$query=mysql_query($sqlSelect);
	
	if(mysql_num_rows($query) > 0)
	{	
		while($row=mysql_fetch_array($query))	
		{	
			$return = $row['noidung'] . "... Xem them tai " . $row['link'];	
			//Kiem tra neu chua co status = 0 thi se insert vao bang tuong ung de gui tin nhan khi trang thai duoc cap nhat status = 1
			if ($row['status'] == 0 ){
				$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES		('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
			}
		}
	}else{
		$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES
	('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
	}	
	
	mysql_query($sqlInsert);
	
	mysql_close($con);
	return "0|". $return;
}
//DA TN Dap an de thi tot nghiep THPT
function check_da($moid, $telco, $service_num, $syntax, $phone, $message)
{
		global $conf;	
		$cuphap = substr($message, 2, strlen($message) - 1);
		$cuphap = trim($cuphap);
		$return = "Tin nhan thanh cong. Hay chac chan soan tin: DA Mon Khoi MaDe gui 8700. Mon: Toan,Ly,Hoa,Van,... Xem them tai http://diemthi.azone.vn/tuyensinh.html";
		
		if(strpos($cuphap,"app") !== false) {
			$cuphap = str_replace(' app','',$cuphap);
			$cuphap = trim($cuphap);
		}
		if(strpos($cuphap,"apa") !== false) {
			$cuphap = str_replace(' apa','',$cuphap);
			$cuphap = trim($cuphap);
		}
		if(strpos($cuphap,"tn") !== false) {
			if(strpos($cuphap,"hoa") !== false) {$cuphap = "TN HOA";}
			if(strpos($cuphap,"dia") !== false) {$cuphap = "TN DIA";}
			if(strpos($cuphap,"sinh") !== false) {$cuphap = "TN SINH";}
			if(strpos($cuphap,"toan") !== false) {$cuphap = "TN TOAN";}
			if(strpos($cuphap,"anh") !== false) {$cuphap = "TN ANH";}
			//DQ TN Tra ve Dap An De Thi Tot Nghiep PTTH
			$sqlSelect = "Select tieude, noidung, link, status From tbl_dapan Where CuPhap = '" . strtoupper($cuphap) . "'";
			$table = "tbl_dapan_mo";
			
		}else{
			if (strpos($cuphap,"cd") !== false){
				if(strpos($cuphap,"toan a") !== false) {$cuphap = "CD TOAN A";}
				if(strpos($cuphap,"hoa a") !== false) {$cuphap = "CD HOA A";}
				if(strpos($cuphap,"ly a") !== false) {$cuphap = "CD LY A";}
				if(strpos($cuphap,"anh a") !== false) {$cuphap = "CD ANH A1";}
				if(strpos($cuphap,"toan b") !== false) {$cuphap = "CD TOAN A";}
				if(strpos($cuphap,"hoa b") !== false) {$cuphap = "CD HOA A";}
				if(strpos($cuphap,"sinh b") !== false) {$cuphap = "CD SINH B";}
				if(strpos($cuphap,"van c") !== false) {$cuphap = "CD VAN C";}
				if(strpos($cuphap,"su c") !== false) {$cuphap = "CD SU C";}
				if(strpos($cuphap,"dia c") !== false) {$cuphap = "CD DIA C";}
				if(strpos($cuphap,"van d") !== false) {$cuphap = "CD VAN D";}
				if(strpos($cuphap,"toan d") !== false) {$cuphap = "CD TOAN A";}
				if(strpos($cuphap,"anh d") !== false) {$cuphap = "CD ANH D";}
				if(strpos($cuphap,"nga d") !== false) {$cuphap = "CD NGA D";}
				if(strpos($cuphap,"phap d") !== false) {$cuphap = "CD PHAP D";}
				if(strpos($cuphap,"duc d") !== false) {$cuphap = "CD DUC D";}
				if(strpos($cuphap,"nhat d") !== false) {$cuphap = "CD NHAT D";}
				if(strpos($cuphap,"trung d") !== false) {$cuphap = "CD TRUNG D";}
			}else{
				if(strpos($cuphap,"toan a") !== false) {$cuphap = "TOAN A";}
				if(strpos($cuphap,"hoa a") !== false) {$cuphap = "HOA A";}
				if(strpos($cuphap,"ly a") !== false) {$cuphap = "LY A";}
				if(strpos($cuphap,"anh a") !== false) {$cuphap = "ANH A1";}
				if(strpos($cuphap,"toan b") !== false) {$cuphap = "TOAN B";}
				if(strpos($cuphap,"hoa b") !== false) {$cuphap = "HOA B";}
				if(strpos($cuphap,"sinh b") !== false) {$cuphap = "SINH B";}
				if(strpos($cuphap,"van c") !== false) {$cuphap = "VAN C";}
				if(strpos($cuphap,"su c") !== false) {$cuphap = "SU C";}
				if(strpos($cuphap,"dia c") !== false) {$cuphap = "DIA C";}
				if(strpos($cuphap,"van d") !== false) {$cuphap = "VAN D";}
				if(strpos($cuphap,"toan d") !== false) {$cuphap = "TOAN D";}
				if(strpos($cuphap,"anh d") !== false) {$cuphap = "ANH D";}
				if(strpos($cuphap,"nga d") !== false) {$cuphap = "NGA D";}
				if(strpos($cuphap,"phap d") !== false) {$cuphap = "PHAP D";}
				if(strpos($cuphap,"duc d") !== false) {$cuphap = "DUC D";}
				if(strpos($cuphap,"nhat d") !== false) {$cuphap = "NHAT D";}
				if(strpos($cuphap,"trung d") !== false) {$cuphap = "TRUNG D";}
			}
			
			//Tra ve Dap An De Thi Dai Hoc
			$sqlSelect = "Select tieude, noidung, link, status From tbl_dapan Where CuPhap = '" . strtoupper($cuphap) . "'";
			$table = "tbl_dapan_mo";
		}
		//$return = $sqlSelect;
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);	
		$query=mysql_query($sqlSelect);
		
		if(mysql_num_rows($query) > 0)
		{	
			while($row=mysql_fetch_array($query))	
			{	
				$return = $row['noidung'] . " tai " . $row['link'];	
				//Kiem tra neu chua co status = 0 thi se insert vao bang tuong ung de gui tin nhan khi trang thai duoc cap nhat status = 1
								
				if ($row['status'] == 0 ){
					$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES		('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
				}
				
			}
		}else{
			$sqlInsert = "Insert Into " . $table . "(mo_id, telco, phone, message, receive_date, status) VALUES
('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), 0)";
	
		}	
		
		mysql_query($sqlInsert);
		
		mysql_close($con);
		return "0|". $return;
}
//DIEM Diem thi lop 10 - cac dich vu cua cu phap DIEM
function check_diem($moid, $telco, $service_num, $syntax, $phone, $message)
{	
		global $conf;	
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);
		
		if(strpos($cuphap,"app") !== false) {
			$cuphap = str_replace('app','',$cuphap);
			$cuphap = trim($cuphap);
		}
		if(strpos($cuphap,"apa") !== false) {
			$cuphap = str_replace(' apa','',$cuphap);
			$cuphap = trim($cuphap);
		}
		$cuphap = substr($message, 4, strlen($message) - 1);
		$cuphap = trim($cuphap);
		$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://diemthi.azone.vn/tuyensinh.html";
		$b = 1;
		if(strpos($cuphap,"kt") !== false) {
			//Tra ket qua ve diem kiem tra lai cua thi sinh
			$cuphap = substr($cuphap, 2, strlen($cuphap) - 1);
			$cuphap = trim($cuphap);
			$tach = explode(" ", $cuphap);
			$matinh = $tach[0];
			$sbd = $tach[1];	
			$b = 0;		
		}else {
			//Tach Ma-Tinh va So-Bao-Danh 
			$tach = explode(" ", $cuphap);
			$matinh = $tach[0];
			$sbd = $tach[1];						
		}
		
		//kiem tra du lieu diem thi cua tinh da co chua
		$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'tbl_diem_" . $matinh . "'";
		$query=mysql_query($sqlcheck);
		$row=mysql_fetch_array($query);
		if ($row[0] == 0) {
			//chua co bang diem tinh -> kiem tra bang luu cho cua tinh
			$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'tbl_diem_" . $matinh . "_mo'";
			$qcheck=mysql_query($sqlcheck);
			$rcheck=mysql_fetch_array($qcheck);
			if ($row[0] == 0) {
				//Tao bang du lieu luu tru cho cua tinh
				$sqlcreate = "CREATE TABLE IF NOT EXISTS `tbl_diem_" . $matinh . "_mo` (
								  `id` bigint(20) NOT NULL AUTO_INCREMENT,
								  `mo_id` bigint(20) NOT NULL,
								  `telco` varchar(255) NOT NULL,
								  `service_num` varchar(255) NOT NULL,
								  `syntax` varchar(255) NOT NULL,  
								  `phone` varchar(255) NOT NULL,  
								  `message` varchar(255) NOT NULL,
								  `receive_date` datetime NOT NULL,
								  `sobaodanh` varchar(255) NOT NULL,
								  `status` tinyint(4) NOT NULL DEFAULT '0',
								  PRIMARY KEY (`id`)
								) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
				$qcreate=mysql_query($sqlcreate);
			}
			//Chen du lieu thi sinh vao bang luu tru cho cua tinh
			$sqlInsert = "Insert Into `tbl_diem_" . $matinh . "_mo`(mo_id, telco, service_num, syntax, phone, message, receive_date, sobaodanh, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), '" .cleanValue($sbd)."', 0)";
			mysql_query($sqlInsert);
			
			if ($b == 0){ $return = "Tin nhan thanh cong! Hay kiem tra lai cu phap nhan tin de nhan ket qua diem thi cua ban. Soan DIEM MaTinh SBD gui 8700";}
		}else{
			//Tra ket qua cho thi sinh
			$sqlSelect = "Select toan, van, tong From `tbl_diem_" . $matinh . "` Where sobaodanh = '" . trim($sbd) . "'";
			$query=mysql_query($sqlSelect);
	
			if(mysql_num_rows($query) > 0)
			{	
				while($row=mysql_fetch_array($query))	
				{	
					$return = "Diem cua ban: Toan ".$row['toan'].", Van ".$row['van'].", Tong diem ".$row['tong'].". Kiem tra soan tin DIEM KT ". $matinh . " ". $sbd ." gui 8700 hoac vao http://diemthi.azone.vn/diem-lop10/";
					if ($b == 0){$return = "Diem cua ban: Toan ".$row['toan'].", Van ".$row['van'].", Tong diem ".$row['tong'].". Hay vao http://diemthi.azone.vn/diem-lop10/ de biet chi tiet";}
				}
			}else{
				$return = "Tin nhan thanh cong! Hay kiem tra lai cu phap nhan tin de nhan ket qua diem thi cua ban. Soan DIEM MaTinh SBD gui 8700";
			}
		}
		
		mysql_close($con);
		return "0|". $return;
}

//DIEM Diem thi tot nghiep thpt - cac dich vu cua cu phap TN
function check_totnghiep($moid, $telco, $service_num, $syntax, $phone, $message)
{			
		global $conf;	
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);
		
		if(strpos($cuphap,"app") !== false) {
			$cuphap = str_replace(' app','',$cuphap);
			$cuphap = trim($cuphap);
		}
		if(strpos($cuphap,"apa") !== false) {
			$cuphap = str_replace(' apa','',$cuphap);
			$cuphap = trim($cuphap);
		}
		$cuphap = substr($message, 2, strlen($message) - 1);
		$cuphap = trim($cuphap);
		$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://diemthi.azone.vn/tuyensinh.html";
		$b = 1;
		if(strpos($cuphap,"kt") !== false) {
			//Tra ket qua ve diem kiem tra lai cua thi sinh
			$cuphap = substr($cuphap, 2, strlen($cuphap) - 1);
			$cuphap = trim($cuphap);
			$tach = explode(" ", $cuphap);
			$matinh = $tach[0];
			$sbd = $tach[1];	
			$b = 0;		
		}else {
			//Tach Ma-Tinh va So-Bao-Danh 
			$tach = explode(" ", $cuphap);
			$matinh = $tach[0];
			$sbd = $tach[1];						
		}
		
		//kiem tra du lieu diem thi cua tinh da co chua
		$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'tbl_diemtn_" . $matinh . "'";
		$query=mysql_query($sqlcheck);
		$row=mysql_fetch_array($query);
		if ($row[0] == 0) {
			//chua co bang diem tinh -> kiem tra bang luu cho cua tinh
			$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'tbl_diemtn_" . $matinh . "_mo'";
			$qcheck=mysql_query($sqlcheck);
			$rcheck=mysql_fetch_array($qcheck);
			if ($row[0] == 0) {
				//Tao bang du lieu luu tru cho cua tinh
				$sqlcreate = "CREATE TABLE IF NOT EXISTS `tbl_diemtn_" . $matinh . "_mo` (
								  `id` bigint(20) NOT NULL AUTO_INCREMENT,
								  `mo_id` bigint(20) NOT NULL,
								  `telco` varchar(255) NOT NULL,
								  `service_num` varchar(255) NOT NULL,
								  `syntax` varchar(255) NOT NULL,  
								  `phone` varchar(255) NOT NULL,  
								  `message` varchar(255) NOT NULL,
								  `receive_date` datetime NOT NULL,
								  `sobaodanh` varchar(255) NOT NULL,
								  `status` tinyint(4) NOT NULL DEFAULT '0',
								  PRIMARY KEY (`id`)
								) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
				$qcreate=mysql_query($sqlcreate);
			}
			//Chen du lieu thi sinh vao bang luu tru cho cua tinh
			$sqlInsert = "Insert Into `tbl_diemtn_" . $matinh . "_mo`(mo_id, telco, service_num, syntax, phone, message, receive_date, sobaodanh, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), '" .cleanValue($sbd)."', 0)";
			mysql_query($sqlInsert);
			
			if ($b == 0){ $return = "Tin nhan thanh cong! Hay kiem tra lai cu phap nhan tin de nhan ket qua diem thi cua ban. Soan TN MaTinh SBD gui 8700";}
		}else{
			//Tra ket qua cho thi sinh
			$sqlSelect = "Select mon1, mon2, mon3, mon4, mon5, mon6, tong From `tbl_diemtn_" . $matinh . "` Where sobaodanh = '" . trim($sbd) . "'";
			$query=mysql_query($sqlSelect);
	
			if(mysql_num_rows($query) > 0)
			{	
				while($row=mysql_fetch_array($query))	
				{	
					$return = "Diem cua ban: m1 ".$row['mon1'].", m2 ".$row['mon2'].", m3 ".$row['mon3'].", m4 ".$row['mon4'].", m5 ".$row['mon5'].", m6 ".$row['mon6'].", Tong diem ".$row['tong'].". Kiem tra soan tin TN KT ". $matinh . " ". $sbd ." gui 8700 hoac vao http://diemthi.azone.vn/diem-tot-nghiep/";
					if ($b == 0){$return = "Diem cua ban: m1 ".$row['mon1'].", m2 ".$row['mon2'].", m3 ".$row['mon3'].", m4 ".$row['mon4'].", m5 ".$row['mon5'].", m6 ".$row['mon6'].", Tong diem ".$row['tong'].". Hay vao http://diemthi.azone.vn/diem-tot-nghiep/ de biet chi tiet";}
				}
			}else{
				$return = "Tin nhan thanh cong! Hay kiem tra lai cu phap nhan tin de nhan ket qua diem thi cua ban. Soan TN MaTinh SBD gui 8700";
			}
		}
		
		mysql_close($con);
		return "0|". $return;
}

//DIEM Diem thi Dai Hoc Cao Dang - cac dich vu cua cu phap TD, XD
//Chua du du lieu de lam 
function check_diemdh($moid, $telco, $service_num, $syntax, $phone, $message)
{	
	
		global $conf;	
		$con = mysql_connect($conf['db_host'], $conf['db_user'], $conf['db_pass']);
		mysql_select_db($conf['db_name']);
		
		if(strpos($cuphap,"app") !== false) {
			$cuphap = str_replace('app','',$cuphap);
			$cuphap = trim($cuphap);
		}
		if(strpos($cuphap,"apa") !== false) {
			$cuphap = str_replace(' apa','',$cuphap);
			$cuphap = trim($cuphap);
		}
		$cuphap = substr($message, 2, strlen($message) - 1);
		$cuphap = trim($cuphap);
		$return = "Tin nhan thanh cong. Du lieu dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://diemthi.azone.vn/tuyensinh.html";
		$b = 1;
		
		if(strpos($cuphap,"kt") !== false) {
			//Tra ket qua ve diem kiem tra lai cua thi sinh
			$cuphap = substr($cuphap, 2, strlen($cuphap) - 1);
			$cuphap = trim($cuphap);
			if (strpos($cuphap," ") == false){
			 	$cuphap = substr($cuphap, 0, 3) . " " . substr($cuphap, 3, 1) . " " . substr($cuphap, 4, strlen($cuphap) - 1);
			}else{
				$tach = explode(" ", $cuphap);				
				$cuphap = substr($tach[0], 0, 3) . " " . substr($tach[0], 3, strlen($tach[0]) -1) . " " . $tach[1];
			}
			$tach = explode(" ", $cuphap);
			$matruongdh = $tach[0];
			$khoi = $tach[1];
			$sbd = $tach[2];	
			$b = 0;		
		}else {
			if (strpos($cuphap," ") == false){
				$cuphap = substr($cuphap, 0, 3) . " " . substr($cuphap, 3, 1) . " " . substr($cuphap, 4, strlen($cuphap) - 1);
			}else{
				$tach = explode(" ", $cuphap);				
				$cuphap = substr($tach[0], 0, 3) . " " . substr($tach[0], 3, strlen($tach[0]) -1) . " " . $tach[1];
			}
			//Tach Ma-Truong va So-Bao-Danh 
			$tach = explode(" ", $cuphap);
			$matruongdh = $tach[0];
			$khoi = $tach[1];
			$sbd = $tach[2];						
		}
		
		//kiem tra du lieu diem thi cua truong dh da co chua
		$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'dhcd_" . strtolower($matruongdh) . "'";
		$query=mysql_query($sqlcheck);
		$row=mysql_fetch_array($query);
		if ($row[0] == 0) {
			//chua co bang diem truong dh -> kiem tra bang luu cho cua truong dh
			$sqlcheck = "SELECT count((1)) AS `ct` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'dhcd" . strtolower($matruongdh) . "_mo'";
			$qcheck=mysql_query($sqlcheck);
			$rcheck=mysql_fetch_array($qcheck);
			if ($row[0] == 0) {
				//Tao bang du lieu luu tru cho cua truong dh
				$sqlcreate = "CREATE TABLE IF NOT EXISTS `dhcd_" . strtolower($matruongdh) . "_mo` (
								  `id` bigint(20) NOT NULL AUTO_INCREMENT,
								  `mo_id` bigint(20) NOT NULL,
								  `telco` varchar(255) NOT NULL,
								  `service_num` varchar(255) NOT NULL,
								  `syntax` varchar(255) NOT NULL,  
								  `phone` varchar(255) NOT NULL,  
								  `message` varchar(255) NOT NULL,
								  `receive_date` datetime NOT NULL,
								  `sobaodanh` varchar(255) NOT NULL,
								  `status` tinyint(4) NOT NULL DEFAULT '0',
								  PRIMARY KEY (`id`)
								) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
				$qcreate=mysql_query($sqlcreate);
			}
			//Chen du lieu thi sinh vao bang luu tru cho cua truong dh
			$sqlInsert = "Insert Into `dhcd_" . strtolower($matruongdh) . "_mo`(mo_id, telco, service_num, syntax, phone, message, receive_date, sobaodanh, status) VALUES('".intval($moid)."', '".cleanValue($telco)."', '" .cleanValue($service_num)."', '".cleanValue($syntax)."', '" .cleanValue($phone)."', '".cleanValue($message)."', NOW(), '" .cleanValue($sbd)."', 0)";
			mysql_query($sqlInsert);
			
			if ($b == 0){ $return = "Tin nhan thanh cong! Hay kiem tra lai cu phap nhan tin de nhan ket qua diem thi cua ban. Soan TD SBD gui 8700";}
			else{
				$return = "Tin nhan thanh cong. Du lieu cua truong " . $matruongdh . " dang cap nhat. Chung toi se gui lai ngay khi co ket qua. Xem them tai http://diemthi.azone.vn/tuyensinh.html";
			}
		}else{
			//Tra ket qua cho thi sinh
			$sqlSelect = "Select dhcd_dm1, dhcd_dm2, dhcd_dm3 From `dhcd_" . strtolower($matruongdh) . "` Where dhcd_sobaodanh = '" . trim($sbd) . "'";
			$query=mysql_query($sqlSelect);
	
			if(mysql_num_rows($query) > 0)
			{	
				while($row=mysql_fetch_array($query))	
				{	
					$return = "Diem thi cua ban: M1: ".$row['dhcd_dm1'].", M2: ".$row['dhcd_dm2'].", M3: ".$row['dhcd_dm3'].". Kiem tra soan tin TD KT " . $matruongdh.$khoi . " " . $sbd . " gui 8700 hoac vao http://diemthi.azone.vn/tuyensinh.html";
					if ($b == 0){$return = "Diem thi cua ban: M1: ".$row['dhcd_dm1'].", M2: ".$row['dhcd_dm2'].", M3: ".$row['dhcd_dm3'].". Hay vao http://diemthi.azone.vn/tuyensinh/box_tra_diem_thi/ de biet chi tiet";}
				}
			}else{
				$return = "Tin nhan thanh cong! Hay kiem tra lai cu phap nhan tin de nhan ket qua diem thi cua ban. Soan TD SBD gui 8700";
			}
		}
		
		mysql_close($con);
		return "0|". $return;
}

$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

$server->service($POST_DATA);

exit();
?>